<?php

namespace app\admin\model;

use think\Model;


class CampaignData extends Model
{

    

    //数据库
    protected $connection = 'database';
    // 表名
    protected $name = 'campaign_data';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'politic_text',
        'status_text',
        'avatar_text'
    ];
    

    
    public function getPoliticList()
    {
        return ['1' => __('Politic 1'), '2' => __('Politic 2'), '3' => __('Politic 3')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getPoliticTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['politic']) ? $data['politic'] : '');
        $list = $this->getPoliticList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function getAvatarTextAttr($value,$data){
        $value = $value ? $value : (isset($data['avatar']) ? $data['avatar'] : '');
        $site = \think\Config::get("site");
        $preg = "/^http(s)?:\\/\\/.+/";
        if(!preg_match($preg,$value))
        {
            $value = $site['file_url'].$value;
        }
        return $value;
    }



    public function user()
    {
        return $this->belongsTo('User', 'site_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
